#include "pd_log.h"
#include "pd_udp_ioc.h"
#include "pd_transport.h"

void pd_udpfd_on_readable(PdIOComponent *ioc)
{
    if (NULL == ioc)
    {
        PD_LOG(WARN, "ioc null pointer");
    }
    else if (NULL == ioc->sock)
    {
        PD_LOG(WARN, "sock null pointer");
    }
    else
    {
        const uint32_t BUFFER_SIZE = 4096;
        char buffer[BUFFER_SIZE];
        int len = sizeof(struct sockaddr);
        int recv_len = 0;
        if (0 == (recv_len = recvfrom(pd_socket_get_fd(ioc->sock), buffer, BUFFER_SIZE,
                    0, (struct sockaddr *)pd_socket_get_peer(ioc->sock), (socklen_t *)&len)))
        {
            PD_LOG(FATAL, "unexpected error, udp fd cannot be error, ioc=%p", ioc);
        }
        else
        {
            PD_LOG(INFO, "read from peer=[%s], [%.*s]", pd_socket_str_peer(ioc->sock), recv_len, buffer);
        }
    }
}

void pd_udpfd_on_writeable(PdIOComponent *ioc)
{
    UNUSED(ioc);
}

void pd_udpfd_on_error(PdIOComponent *ioc)
{
    PD_LOG(FATAL, "unexpected error, udp fd cannot be error, ioc=%p", ioc);
}

